<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="时间">
        <el-date-picker
          clearable
          size="small"
          style="width: 100%"
          v-if="startTime != '1900-03-23 18:35:23'"
          v-model="startTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="开始时间"
          @blur="fetchData()"
        ></el-date-picker>
        <el-date-picker
          clearable
          size="small"
          style="width: 100%"
          v-else
          v-model="startNull"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="开始时间"
          @blur="fetchData()"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="~">
        <el-date-picker
          clearable
          size="small"
          style="width: 100%"
          v-if="endTime != '2999-03-24 18:35:23'"
          v-model="endTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="结束时间"
          @blur="fetchData()"
        ></el-date-picker>
        <el-date-picker
          clearable
          size="small"
          style="width: 100%"
          v-else
          v-model="endNull"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="结束时间"
          @blur="fetchData()"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="充电站点">
        <span>
          <el-select
            v-model="stationName"
            placeholder="请选择"
            clearable
            @change="fetchData()"
          >
            <el-option
              v-for="item in optionList"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </span>
      </el-form-item>

      <!-- <el-form-item label="用户状态">
          <el-select v-model="searchObj.status" placeholder="请选择" clearable>
            <el-option label="正常" value="1" />
            <el-option label="锁定" value="0" />
          </el-select>
        </el-form-item> -->

      <el-button
        type="primary"
        icon="el-icon-search"
        @click="fetchData()"
        style="margin-left: 163px"
      >
        查询
      </el-button>
      <el-button type="save" size="main" @click="save()">新增</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
      <el-button style="margin-right: 0px" type="primary" @click="exportThis()">
        导出
      </el-button>
    </el-form>

    <!-- 列表 -->
    <el-table :data="list" border stripe>
      <el-table-column label="#" width="50">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="addressSite" label="充电站点名称" />
      <el-table-column prop="number" label="充电桩编号" />
      <el-table-column prop="address" label="地址位置" />
      <el-table-column prop="hiddenType" label="隐患类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.hiddenType === 0" type="danger" size="mini">
            重大隐患
          </el-tag>
          <el-tag
            v-else-if="scope.row.hiddenType === 1"
            type="danger"
            size="mini"
          >
            紧急隐患
          </el-tag>
          <el-tag
            v-else-if="scope.row.hiddenType === 2"
            type="danger"
            size="mini"
          >
            一般隐患
          </el-tag>
          <el-tag v-else type="danger" size="mini">类型无法确定</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="rank" label="隐患优先级">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.rank === 0" type="seuccse" size="mini">
            最高级
          </el-tag>
          <el-tag v-else-if="scope.row.rank === 1" type="seuccse" size="mini">
            高级
          </el-tag>
          <el-tag v-else-if="scope.row.rank === 2" type="seuccse" size="mini">
            中级
          </el-tag>
          <el-tag v-else-if="scope.row.rank === 3" type="seuccse" size="mini">
            低级
          </el-tag>
          <el-tag v-else type="danger" size="mini">状态无法确定</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="content" label="隐患内容" />
      <el-table-column prop="status" label="处理状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="seuccse" size="mini">
            登记
          </el-tag>
          <el-tag v-if="scope.row.status === 1" type="seuccse" size="mini">
            已派单
          </el-tag>
          <el-tag v-if="scope.row.status === 2" type="seuccse" size="mini">
            未处理
          </el-tag>
          <el-tag v-else-if="scope.row.status === 3" type="seuccse" size="mini">
            处理中
          </el-tag>
          <el-tag v-else-if="scope.row.status === 4" type="seuccse" size="mini">
            已处理
          </el-tag>
          <el-tag v-else-if="scope.row.status === 5" type="seuccse" size="mini">
            已完成
          </el-tag>

          <!-- <el-tag v-else type="danger" size="mini">状态无法确定</el-tag> -->
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="登记时间" />
      <el-table-column label="操作" align="left" width="375">
        <template slot-scope="scope">
          <el-button type="primary" size="mini">
            <router-link
              :to="
                '/charging-pile/hitch_detail/' +
                scope.row.id +
                '/' +
                scope.row.status
              "
            >
              查看
            </router-link>
          </el-button>
          <el-button
            v-if="scope.row.status === 0 && scope.row.status != 1"
            type="primary"
            size="mini"
            @click="findDealByHitchId(scope.row.id)"
          >
            派发
          </el-button>
          <el-button
            v-if="scope.row.status === 1 && scope.row.status != 4"
            type="primary"
            size="mini"
            @click="setPersonName(scope.row.id)"
          >
            撤回
          </el-button>
          <el-button
            v-if="scope.row.status === 0 && scope.row.status != 1"
            type="primary"
            size="mini"
            @click="isHandle(scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            v-if="scope.row.status === 1 && scope.row.status != 4"
            type="danger"
            size="mini"
            @click="edd_change(scope.row.id)"
          >
            重派
          </el-button>
          <el-button
            v-if="scope.row.status === 0 && scope.row.status != 1"
            type="primary"
            size="mini"
            @click="removeById(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[1, 5, 10, 15, 20]"
      style="padding: 30px 0"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
    <!-- 办理 -->
    <el-dialog title="派发" :visible.sync="dealIsTrue">
      <el-form ref="form" class="demo-form-inline" v-if="dealType == 1">
        <el-form-item label="处理人">
          <el-select v-model="personList.personName" placeholder="请选择">
            <el-option
              v-for="item in solvePersonList"
              :key="item.id"
              :label="item.personName"
              :value="item.personName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务内容">
          <el-input type="textarea" v-model="personList.assContent" />
        </el-form-item>
        <el-form-item label="预计完成时间">
          <el-date-picker
            clearable
            size="small"
            style="width: 100%"
            v-model="personList.estimate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="操作">
          <el-button type="primary" @click="addPerson()">派发</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import listApi from '@/api/charging-pile/hidden_manage'
export default {
  data() {
    //let BASE_API = process.env.VUE_APP_BASE_API
    return {
      list: null, // 数据列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 5, // 每页记录数
      searchObj: {}, // 查询条件
      personList: {
        id: 0,
      },
      startTime: '1900-03-23 18:35:23',
      endTime: '2999-03-24 18:35:23',
      startNull: '',
      endNull: '',
      optionList: [],
      stationName: '',
      rankList: [],
      rank: '',
      types: '1',
      updateHandle: {},
      handlePersonList: [],
      loginRecordList: [], //会员登录日志
      dialogTableVisible: false, //对话框是否显示
      dealIsTrue: false, // 是否显示办理弹窗
      dealType: 0, // 对应显示办理类型 1办理 2 重新办理
      //uploadUrl: BASE_API + ':8130/api/oss/file/upload', //文件上传地址
      solvePersonList: {},
      PersonHiddenList: [],

      dialogVisible: false, //文件上传对话框是否显示
      BASE_API: process.env.VUE_APP_BASE_API, //获取后端接口地址
    }
  },
  created() {
    console.log("进来了");
    this.fetchData()
    this.getOptins()
  },
  methods: {
    getOptins() {
      listApi.findChargingStations().then((res) => {
        // console.log(res)
        this.optionList = res.data.options
      })
      listApi.findCpHandleCode().then((res) => {
        // console.log(res)
        this.rankList = res.data.code
      })

      listApi.solvePerson().then((res) => {
        // console.log(res)
        this.solvePersonList = res.data.list
      })
      // listApi.findHandlePerson().then((res) => {
      //   console.log(res)
      //   this.handlePersonList = res.data.code
      // })
    },
    changePageSize(pagesize) {
      this.limit = pagesize
      this.fetchData()
    },
    changeCurrentPage(pages) {
      this.page = pages
      this.fetchData()
    },
    fetchData() {
      if (this.startNull == null) {
        this.startNull = ''
      }
      if (this.endNull == null) {
        this.endNull = ''
      }
      if (this.stationName == '') {
        this.stationName = '请选择'
      }
      if (this.startNull == '' && this.endNull == '') {
        listApi
          .findAll(
            this.page,
            this.limit,
            this.startTime,
            this.endTime,
            this.stationName
          )
          .then((res) => {
            console.log(res)
            this.list = res.data.list
            this.total = res.count
            // console.log(this.total)
          })
        return
      }
      if (this.startNull != '' && this.endNull != '') {
        listApi
          .findAll(
            this.page,
            this.limit,
            this.startNull,
            this.endNull,
            this.stationName
          )
          .then((res) => {
            // console.log(res)
            this.list = res.data.list
            this.total = res.count
          })
        return
      }
      if (this.endNull == '') {
        listApi
          .findAll(
            this.page,
            this.limit,
            this.startNull,
            this.endTime,
            this.stationName
          )
          .then((res) => {
            // console.log(res)
            this.list = res.data.list
            this.total = res.count
          })
        return
      }
      if (this.startNull == '') {
        listApi
          .findAll(
            this.page,
            this.limit,
            this.startTime,
            this.endNull,
            this.stationName
          )
          .then((res) => {
            // console.log(res)
            this.list = res.data.list
            this.total = res.count
          })
        return
      }
    },
    resetData() {
      // 还原表单
      this.createTime = ''

      this.updateTime = ''

      this.stationName = '请选择'

      //this.rank = '请选择'
      // 重新查询
      this.fetchData()
    },
    findDealByHitchId(id) {
      sessionStorage.setItem('hiddenId', id)
      listApi.fetchDataById(id).then((res) => {
        this.dealType = 1
        this.dealIsTrue = true
      })
    },
    addPerson() {
      console.log(this.personList)
      this.personList.id = sessionStorage.getItem('hiddenId')
      listApi.serPerson(this.personList).then((res) => {
        if (res.message == 'ok') {
          this.dealIsTrue = false
          this.fetchData()
          this.$message.success('以为您派发...')
        } else {
          this.$message.error('派发失败...')
        }
      })
    },
    //Excel数据导出
    exportThis() {
      window.location.href = this.BASE_API + '/cpHiddenInfo/export'
    },

    // 根据id删除数据
    removeById(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          listApi.removeById(id).then((response) => {
            this.$message.success(response.message)
            this.fetchData()
          })
        })
        .catch((error) => {
          this.$message.info('取消删除')
        })
    },

    setPersonName(id) {
      this.$confirm('此操作将会撤回处理人, 是否继续?', '提示', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          listApi.setPersonName(id).then((response) => {
            this.$message.success(response.message)
            this.fetchData()
          })
        })
        .catch((error) => {
          this.$message.info('取消撤回')
        })
    },

    save() {
      this.$router.push({ path: '/charging-pile/save' })
    },
  },
}
</script>
